package com.flyduck.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyduck.common.lang.Result;
import com.flyduck.service.SysUserService;
import com.flyduck.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Arrays;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author flyduck
 * @since 2024-05-27
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    @PreAuthorize("hasAuthority('sys:user:list')")
    @GetMapping("/info/{id}")
    public Result<SysUserDetailsRespVO> getUserDetailsById(@PathVariable(name = "id") Long id){
        SysUserDetailsRespVO sysUserDetailsRespVO = sysUserService.getUserDetailsById(id);
        return Result.success(sysUserDetailsRespVO);
    }

    @PreAuthorize("hasAuthority('sys:user:list')")
    @GetMapping("/list")
    public Result<Page<SysUserRespVO>> getUserPage(SysUserQueryReqVO sysUserQueryReqVO){
        Page page = getPage();
        Page<SysUserRespVO> sysUserRespVOPage = sysUserService.getUserPageBySysUserQueryReqVO(page,sysUserQueryReqVO);
        return Result.success(sysUserRespVOPage);
    }

    @PreAuthorize("hasAuthority('sys:user:save')")
    @PostMapping("/save")
    public Result<Void> saveUser(@Validated @RequestBody SysUserSaveReqVO sysUserSaveReqVO){
        sysUserService.saveUser(sysUserSaveReqVO);
        return Result.success();
    }

    @PreAuthorize("hasAuthority('sys:user:update')")
    @PostMapping("/update")
    public Result<Void> updateUser(@Validated @RequestBody SysUserUpdateReqVO sysUserUpdateReqVO){
        sysUserService.updateUser(sysUserUpdateReqVO);
        return Result.success();
    }

    @PreAuthorize("hasAuthority('sys:user:delete')")
    @PostMapping("/delete")
    public Result<Void> deleteByIds(@RequestBody Long[] ids){
        sysUserService.deleteByIds(Arrays.asList(ids));
        return Result.success();
    }

    @PreAuthorize("hasAuthority('sys:user:role')")
    @PostMapping("/role/{userId}")
    public Result<Void> assignRolesToUser(@PathVariable(name = "userId") Long userId,@RequestBody Long[] roleIds){
        sysUserService.assignRolesToUser(userId,Arrays.asList(roleIds));
        return Result.success();
    }

    @PreAuthorize("hasAuthority('sys:user:repass')")
    @PostMapping("/repass/{userId}")
    public Result<Void> repassByUserId(@PathVariable(name = "userId") Long userId){
        sysUserService.repassByUserId(userId);
        return Result.success();
    }

    @PostMapping("/updatePass")
    public Result<Void> updatePassByUsername(@Validated @RequestBody SysUserPasswordUpdateReqVO sysUserPasswordUpdateReqVO, Principal principal){
        sysUserService.updatePassByUsername(sysUserPasswordUpdateReqVO,principal.getName());
        return Result.success();
    }

}
